<?php
$iid=Yii::app()->request->getParam('iid', 0);
$cate=Yii::app()->request->getParam('cate', 0);

$uid=Yii::app()->user->id;
$session=new CHttpSession();
$session->open();
$session['i']=1;

switch($iid){
    case 10:
    case 12:
        $article=Article::model()->find('cid='.$iid);
        if(empty($article)){
            $article=new Article;
            $article->cid=$iid;
            $article->uid=$uid;
        }

        if(isset($_POST['Article'])){
            $article->attributes=$_POST['Article'];
            $article->content=$_POST['Article']['content'];
            if($article->validate())
                $article->save();
        }


        $cs=Yii::app()->clientScript;
        $cs->registerCoreScript('jquery');
        $dir = Yii::getPathOfAlias('ext.ueditor');
        $baseUrl = Yii::app()->getAssetManager()->publish($dir);
        $cs->registerScriptFile($baseUrl.'/editor_all.js',CClientScript::POS_HEAD );
        $cs->registerScriptFile($this->createUrl('site/ueditor', array('_'=>md5(rand(),rand()))),CClientScript::POS_HEAD );
        $dir = Yii::getPathOfAlias('ext.ueditor.themes.default');
        $baseUrl = Yii::app()->getAssetManager()->publish($dir);
        $cs->registerCssFile($baseUrl.'/ueditor.css');
        $cs->registerScript('ueditor', 'var ue = new UE.ui.Editor();ue.render(\'Article_content\');', CClientScript::POS_READY);
?>
        <div class="detailContent" style="min-height: 1335px">
            <?php $form=$this->beginWidget('CActiveForm', array(
            'id'=>'article-form',
            'enableAjaxValidation'=>false,
        )); ?>

            <?php echo $form->errorSummary($article); ?>


            <div class="row">
                <?php echo $form->textArea($article,'content'); ?>
                <?php echo $form->error($article,'content'); ?>
            </div>


            <div class="row buttons">
                <?php echo CHtml::submitButton($article->isNewRecord ? '保存' : '保存'); ?>
            </div>

            <?php $this->endWidget(); ?>

            <div class="clear"></div>
        </div>


<?php
        break;
    case 11:
    case 13:
        $id=Yii::app()->request->getParam('id', 0);
        $article=Article::model()->find('cid='.$iid.' AND aid='.intval($id));
        $session['cate']=$cate;
        $session['iid']=$iid;
        $session['i']=1;

        if(empty($article)){
            $article=new Article;
            $article->cid=$iid;
        }
        $do=Yii::app()->request->getParam('do', '');
        $url=$this->createUrl('manage/index',array('cate'=>$cate,'iid'=>$iid));
        if($do=='delete'){
            if(isset($article->uid) && $article->uid==$uid){
                $article->enabled=0;
                if($article->save()){
                    $this->oz_info('文章删除成功',$url);
                    return;
                }
            } else {
                $this->oz_error('只有发布者可以删除该文章',$url);
                return;
            }
        }elseif($do=='new'){

        }

        $criteria=new CDbCriteria;
        $criteria->condition='`cid`=:cid AND enabled=1';
        $criteria->order='sort ASC , createtime DESC';
        $criteria->params=array(':cid'=>$iid);
        $dataProvider=new CActiveDataProvider('Article',array(
            'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));

        if(isset($_POST['Article'])){
            $article->attributes=$_POST['Article'];
            $article->content=$_POST['Article']['content'];
            $article->sort=(!isset($_POST['Article']['sort'])) ? 0 : intval($_POST['Article']['sort']);
            $article->excerpt=isset($_POST['Article']['excerpt']) ? $_POST['Article']['excerpt'] : '';
            $article->uid=$uid;
            $article->audit=1;
            $article->grade=0;
            $article->createtime=empty($article->createtime) ? date("Y-m-d H:i:s") : $article->createtime;
            $article->updatetime=date("Y-m-d H:i:s");
            if($article->cid == 13){
                $article->title=$article->content;
            }
//            $article->sort=0;
            $article->type=1;
            if($article->isNewRecord){
                $msg='添加成功';
            }else{
                $msg='保存成功';
            }

            if($article->validate()){
                if($article->save()){
                    $this->oz_info($msg, $url);
                    return;
                }
            }
        }

?>
        <style type="text/css">
            div.row label{
                width: 70px;
                display: inline-block;
            }
            div.row span.msg{
                margin-left: 10px;
                color: #c2c2c2;
            }
            div.row input{
                height: 30px;
                font-size: 14px;
                margin: 5px;
            }
            div.row textarea{
                font-size: 14px;
                margin: 5px;
            }
            div.hr{
                border-bottom: 1px solid #b3b3b3;
                margin-bottom: 20px;
            }
        </style>
        <div class="wp detailContent22">
            <?php if($article->isNewRecord===true && $do!='new'): ?>
            <table width="100%" cellspacing="0" cellpadding="0" border="0" class="table3 paddingbotton80 ">
                <tbody>
                <tr>
                    <th bgcolor="#efefef" align="center" class="th1">标题</th>
                    <th width="20%" bgcolor="#efefef" align="center" class="th1">发布时间</th>
                    <th width="18%" bgcolor="#efefef" align="center">操作指令</th>
                </tr>
                    <?php
                if(!empty($dataProvider->data))
                    $this->widget('zii.widgets.CListView', array(
                        'dataProvider'=>$dataProvider,
                        'itemView'=>'xhzc/list',
                        'ajaxUpdate'=>false,
                        'template'=>"{items}\n{pager}" ,
                        //        'itemsTagName'=>'ul',
                        //        'itemsCssClass'=>'tb',
                    ));
                ?>
                </tbody>
            </table>
            <script language="JavaScript">
                function new_a(){
                    window.location.href="<?php echo $this->createUrl('manage/index',array('cate'=>$cate,'iid'=>$iid,'do'=>'new'));?>";
                }
            </script>

            <?php
            echo CHtml::button('新增',array('onclick'=>'javascript:new_a();')) ;
            elseif(empty($article->isNewRecord) || $do=='new'):
            $this->new_editor('Article_content');
            ?>

            <?php $form=$this->beginWidget('CActiveForm', array(
                'id'=>'article-form',
                'enableAjaxValidation'=>false,
            )); ?>

            <?php echo $form->errorSummary($article); ?>

            <?php if($iid==11): ?>

                <div class="row">
                    <?php echo $form->labelEx($article,($iid==11) ? 'title' : '纪要标题'); ?>
                    <?php if($iid==11): ?>
                    <?php echo $form->textField($article,'title', array('size'=>'60')); ?>
                    <?php else: ?>
                    <?php echo $form->textArea($article,'title', array('style'=>'height:125px;width:645px;')); ?>
                    <?php endif; ?>
                    <?php echo $form->error($article,'title'); ?>
                </div>

                <div class="row">
                    <?php echo $form->labelEx($article,'excerpt'); ?>
                    <?php echo $form->textField($article,'excerpt', array('size'=>'40')); ?>
                    <?php echo $form->error($article,'excerpt'); ?>
                </div>
            <?php endif; ?>

                <div class="row">
                    <?php echo $form->labelEx($article,($iid==11) ? 'content': '纪要内容'); ?>
                    <?php echo $form->textArea($article,'content'); ?>
                    <?php echo $form->error($article,'content'); ?>
                </div>



                <div class="row">
                    <?php echo $form->labelEx($article,'sort'); ?>
                    <?php echo $form->textField($article,'sort'); ?>
                    <?php echo $form->error($article,'sort'); ?>
                </div>


            <div class="row buttons">
                <?php echo CHtml::submitButton($article->isNewRecord ? '添加文章' : '保存文章'); ?>
            </div>

            <?php $this->endWidget(); ?>

            <?php endif; ?>

            <div class="clear"></div>
        </div>


<?php
        break;
    case 12:
?>


<?php
        break;
    case 13:
?>


<?php
        break;
}